<!--
 * @Author: YaYa
 * @Date: 2022-10-12 14:11:15
 * @LastEditTime: 2022-10-18 23:29:17
 * @LastEditors: YaYa
 * @Description:
 * @FilePath: \HeyTea-ui\src\pages\admin\carousel\List.vue
 * 别乱动！
-->
<template>
  <!-- 轮播配置页面 -->
  <div>
    <!-- 第一行表单控件开始 -->
    <el-form :inline="true" class="demo - form - inline">
      <el-form-item>
        <el-button type="primary" @click="toAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 第一行表单控件结束 -->
    <!-- 第二行表单开始 -->
    <el-table :data="carousel" style="width: 100%">
      <el-table-column type="index" label="序号" width="180" />
      <el-table-column prop="name" label="轮播图名称" width="180" />
      <el-table-column prop="introduce" label="轮播图描述" />
      <el-table-column prop="url" label="轮播图图片" width="180">
        <template slot-scope="scope">
          <!-- {{ scope.row.url }} -->
          <el-popover trigger="hover" placement="right">
            <img
              slot="reference"
              fit="contain"
              width="30px"
              :src="scope.row.url || ''"
              alt="图片加载失败"
            >
            <img
              width="200px"
              fit="contain"
              :src="scope.row.url || ''"
              alt="图片加载失败"
            >
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column prop="status" label="状态" width="180">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '正常'" type="success">
            正常
          </el-tag>
          <el-tag v-if="scope.row.status === '停用'" type="danger">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="date" label="操作" width="180">
        <template slot-scope="scope">
          <a class="toEdit" @click="toEdit(scope.row)">编辑</a>
          <a class="toDelete" @click="toDelete(scope.row.id)">删除</a>
          <!-- <a class="toDetails" @click="toDetails(scope.row)">停用</a> -->
          <a
            v-if="scope.row.status === '停用'"
            class="onLine"
            @click="changeStatuss(scope.row.id)"
          >启用</a>
          <a v-else class="offLine" @click="changeStatus(scope.row)">停用</a>
        </template>
      </el-table-column>
    </el-table>
    <!-- 第二行表单结束 -->
    <!-- 模态框开始 -->
    <el-dialog title="新增轮播图" :visible.sync="visible" @close="resetForm">
      <el-form ref="myForm" :model="form">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>

        <el-form-item label="图片" :label-width="formLabelWidth" prop="url">
          <el-upload
            class="avatar-uploader"
            action="http://121.4.83.32:7001/UploadControlle/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="产品描述" :label-width="formLabelWidth">
          <el-input v-model="form.introduce" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>

<script>
import { get, post } from '@/utils/request'
import { uploadFileURL } from '@/utils/config'
export default {
  data() {
    return {
      form: {},
      carousel: [],
      visible: false,
      uploadFileURL,
      formLabelWidth: '120px',
      imageUrl: ''
    }
  },
  computed: {},
  created() {
    this.getAllCarousel()
  },
  mounted() {},
  methods: {
    changeStatus(row) {
      if (row.status === '正常') {
        row.status = '停用'
      } else if (row.status === '停用') {
        row.status = '启用'
      }
    },
    changeStatuss() {
      this.$message({
        type: 'success',
        message: '更新成功'
      })
      this.getAllCarousel()
    },
    // changeStatuss(row) {
    //   if (row.status == "停用") {
    //     row.status = "正常";
    //   } else if (row.status == "停用") {
    //     row.status = "启用";
    //   }
    // },
    // 添加
    async toSubmit() {
      const res = await post('/carousel/saveOrUpdate', this.form)
      // console.log(res, "ffff");
      this.visible = false
      this.$message({
        type: 'success',
        message: res.message
      })
      this.getAllCarousel()
    },
    toAdd() {
      this.visible = true
    },

    handleAvatarSuccess(res, file) {
      // console.log(res.data, "hhhh");
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.url = res.data
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    resetForm() {
      this.imageUrl = ''
    },
    // 表单校验规则
    // rules: {
    //   name: [{ required: true, message: "请输入名称", trigger: "blur" }],
    //   // url: [{ required: true, message: "选择图片", trigger: "blur" }],
    // },
    async getAllCarousel() {
      const res = await get('/carousel/findAll')
      console.log(res.data)
      this.carousel = res.data
    },
    // 跳到详情页面

    // 去删除
    async toDelete(id) {
      // console.log(id);
      // this.thisId = row.id;
      // console.log(thisId);
      this.$confirm('此操作将永久删除轮播图信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })

      const res = await get('/carousel/deleteById', { id })

      console.log(res, '删除')
      // this.getpageQuery();
    },
    // 编辑顾客
    toEdit(row) {
      this.form = { ...row }
      this.title = '修改用户信息'
      this.visible = true
    }
  }
}
</script>
<style scoped>
</style>
